home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Development / Source / Telnet 2.6.1d1 4⁄26⁄94 Folder / source / network / mydnr.c < prev    next >
Text File  |  1994-04-16  |  5KB  |  202 lines

  1. /*
  2. *   Util.c
  3. *   utility library for use with the Network kernel
  4. *
  5. *   version 2, full session layer, TK started 6/17/87
  6. *****************************************************************
  7. *    NCSA Telnet for the Macintosh                                *
  8. *                                                                *
  9. *    National Center for Supercomputing Applications                *
  10. *    Software Development Group                                    *
  11. *    152 Computing Applications Building                            *
  12. *    605 E. Springfield Ave.                                        *
  13. *    Champaign, IL  61820                                        *
  14. *                                                                *
  15. *    Copyright (c) 1986-1992,                                    *
  16. *    Board of Trustees of the University of Illinois                *
  17. *****************************************************************
  18. */
  19.  
  20. #ifdef MPW
  21. #pragma segment DNR
  22. #endif
  23.  
  24. #include <OSUtils.h>
  25. #include <Folders.h>
  26. #include <ctype.h>
  27.  
  28. #include "TelnetHeader.h"
  29. #include "InternalEvents.h"
  30. #include "bkgr.proto.h"
  31. #include "AddressXlation.h"
  32. #include "telneterrors.h"
  33. #include "netevent.proto.h"
  34. #include "mydnr.proto.h"
  35. #include "Connections.proto.h"
  36.  
  37. extern long MyA5;
  38.  
  39. typedef    struct {
  40.     short
  41.         screen;
  42.     long
  43.         MyA5;
  44.     OSErr
  45.         theError;
  46.     Str63
  47.         hostname;
  48.     struct hostInfo
  49.         *hinfo;
  50. }    DNRDelayStruct;
  51.  
  52. pascal void DNRDone(struct hostInfo *hostInfoPtr, DNRDelayStruct *info);
  53. pascal void DNRDoneInit(struct hostInfo *hostInfoPtr, DNRDelayStruct *info);
  54. PROTO_UPP(DNRDoneInit, Result);
  55.  
  56. /**********************************************************************
  57.  * DotToNum - turn an address in dotted decimal into an internet address
  58.  * returns True if the conversion was successful.  This routine is
  59.  * somewhat limited, in that it will accept only four octets, and does
  60.  * not permit the abbreviated forms for class A and B networks. <- Steve Dorner
  61.  **********************************************************************/
  62. Boolean DotToNum(BytePtr string,ip_addr *nPtr)
  63. {
  64.   unsigned long    address=0;
  65.   short            b=0;
  66.   BytePtr         cp;
  67.   short            dotcount=0;
  68.   
  69.   /*
  70.    * allow leading spaces
  71.    */
  72.   for (cp=string+1;cp<=string+*string;cp++) if (*cp!=' ') break;
  73.   
  74.   /*
  75.    * the address
  76.    */
  77.   for (;cp<=string+*string;cp++)
  78.   {
  79.     if (*cp=='.')
  80.     {
  81.       if (++dotcount > 3) return (FALSE); /* only 4 octets allowed */
  82.       address <<= 8;
  83.       address |= b;
  84.       b=0;
  85.     }
  86.     else if (isdigit(*cp))
  87.     {
  88.       b *= 10;
  89.       b += (*cp - '0');
  90.       if (b>255) return (FALSE);          /* keep it under 256 */
  91.     }
  92.     else if (*cp==' ')                    /* allow trailing spaces */
  93.       break;
  94.     else
  95.       return (FALSE);                     /* periods or digits ONLY */
  96.   }
  97.   
  98.   /*
  99.    * final checks, assignment
  100.    */
  101.   if (dotcount!=3) return (FALSE);
  102.   address <<= 8;
  103.   address |= b;
  104.   *nPtr = (ip_addr) address;
  105.   return(TRUE);
  106. }
  107.  
  108. /**************************************************************************/
  109. /*    For FTP to look up the transfer options to use when running */
  110. Boolean    TranslateIPtoDNSname(ip_addr ipnum, StringPtr machineName)
  111. {
  112.     #pragma unused (ipnum, machineName)
  113.     // NEED TO IMPLEMENT THIS BUGG
  114.     return(FALSE);
  115.  
  116. }
  117.  
  118. /*********************************************************************/
  119. pascal void DNRDone(struct hostInfo *hostInfoPtr, DNRDelayStruct *info)
  120. {
  121.     #pragma unused (hostInfoPtr)
  122.     netputevent(USERCLASS,DOMAIN_DONE,info->screen, (long)info);
  123. }
  124.  
  125. SIMPLE_UPP(DNRDoneInit, Result);
  126. pascal void DNRDoneInit(struct hostInfo *hostInfoPtr, DNRDelayStruct *info)
  127. {
  128.     long saveA5;
  129.     DNRDelayStruct *mptr;
  130.     struct hostInfo *hptr;
  131.     
  132.     mptr = info;
  133.     hptr = hostInfoPtr;
  134.  
  135. #ifndef    __powerpc__
  136.     saveA5 = SetA5(mptr->MyA5);
  137. #endif
  138.  
  139.     DNRDone(hptr, mptr);
  140.     
  141. #ifndef    __powerpc__
  142.     SetA5(saveA5);
  143. #endif
  144. }
  145.  
  146. OSErr    DoTheDNR(StringPtr hostname, short window)
  147. {
  148.     DNRDelayStruct        *Info;
  149.     struct hostInfo        *HInfo;
  150.     ip_addr                ip;
  151.     
  152.     Info = (DNRDelayStruct *)NewPtrClear(sizeof(DNRDelayStruct));
  153.     HInfo = (struct hostInfo *)NewPtrClear(sizeof(struct hostInfo));
  154.     
  155.     if ((Info == NULL) || (HInfo == NULL)) return(memFullErr);
  156.     
  157.     Info->screen = window;
  158. #ifndef __powerpc__
  159.     Info->MyA5 = MyA5;
  160. #endif
  161.     Info->hinfo = HInfo;
  162.         
  163.     BlockMove(hostname, Info->hostname, Length(hostname)+1);
  164.     
  165.     if (DotToNum(hostname, &ip)) {
  166.         Info->hinfo->rtnCode = noErr;
  167.         Info->hinfo->addr[0] = ip;
  168.         DNRDone(HInfo, Info);
  169.         return(noErr);
  170.         }
  171.         
  172.     PtoCstr(Info->hostname);
  173.     Info->theError = StrToAddr((char *)Info->hostname, HInfo,
  174.                                 DNRDoneInitUPP, (Ptr)Info);
  175.     
  176.     if ((Info->theError != cacheFault) && (Info->theError != inProgress))
  177.         DNRDone(HInfo, Info);
  178.     
  179.     return(noErr);
  180. }
  181.  
  182. //    The event queue routines send us the screen number that DNRDone sent.  We demangle
  183. //    this mess of data and call CompleteConnectionOpening to do all of the port and screen
  184. //    stuff that we shouldn't know about at this level.  We are merely a non-interrupt level
  185. //    flow control point. (i.e. I would do this from DNRDone, but that's interrupt time)
  186. void    HandleDomainDoneMessage(short screen, long data2)
  187. {
  188.     DNRDelayStruct    *MyData = (DNRDelayStruct *)data2;
  189.     ip_addr            the_IP;
  190.     OSErr            theErr;        // The error, if any
  191.     
  192.     the_IP = MyData->hinfo->addr[0];
  193.     theErr = MyData->hinfo->rtnCode;
  194.     
  195.     CompleteConnectionOpening(screen, the_IP, theErr, MyData->hinfo->cname);
  196.  
  197.     //    We also dispose of the DNR memory allocations
  198.         
  199.     DisposePtr((Ptr)MyData->hinfo);
  200.     DisposePtr((Ptr)MyData);
  201. }
  202.